package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC661 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        int[][] directions = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};

        public int[][] imageSmoother(int[][] img) {
            int m = img.length, n = img[0].length;
            int[][] ans = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    int curSum = 0, cnt = 0;
                    for (int[] direction : directions) {
                        int nextX = i + direction[0], nextY = j + direction[1];
                        if (nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) continue;
                        curSum += img[nextX][nextY];
                        cnt++;
                    }
                    ans[i][j] = curSum / cnt;
                }
            }

            return ans;
        }
    }

    public void run() {
        System.out.println(new Solution().imageSmoother(InputUtil.toDoubleIntegerArray("[[1,1,1],[1,0,1],[1,1,1]]")));
        System.out.println(new Solution().imageSmoother(InputUtil.toDoubleIntegerArray("[[100,200,100],[200,50,200],[100,200,100]]")));
    }

    public static void main(String[] args) throws Exception {
        LC661 an = new LC661();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
